﻿using System;
using Windows.UI.Xaml.Data;

namespace ShopPowerTemplate.Common
{
    /// <summary>
    /// Value converter that translates true to false and vice versa.
    /// </summary>
    public sealed class TimeToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string result = string.Empty;
            if (value is DateTime)
            {
                DateTime date = ((DateTime)value);
                if (date != DateTime.MinValue)
                    result = date.ToString("d");
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            DateTime result = DateTime.MinValue;
            if (value is string)
            {
                string dateString = ((string)value);
                if (!string.IsNullOrWhiteSpace(dateString))
                    DateTime.TryParse(dateString, out result);
            }
            return result;
        }
    }
}
